package com.whfc.ms.shiro;

import com.whfc.common.result.Result;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.JSONUtil;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2019/7/22 16:02
 */
public class MsUserFilter extends FormAuthenticationFilter {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
        try {
            HttpServletResponse httpServletResponse = (HttpServletResponse) response;
            Result result = new Result();
            result.setCode(ResultEnum.TOKEN_EXPIRED.getCode());
            result.setMsg(ResultEnum.TOKEN_EXPIRED.getMessage());
            httpServletResponse.setCharacterEncoding("utf-8");
            httpServletResponse.getWriter().write(JSONUtil.toString(result));
        } catch (IOException ex) {
            logger.error(ex.getMessage(), ex);
        }
        return false;
    }
}
